﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.IO;

namespace WebLayer
{
    public enum LogoutType 
    { 
        ForgetCookies,  /* Logout is done via erasing session cookies. This may be insecure. */
        RequestLogout   /* Logout is done via logout request to site */
    };
   
    public class LoginToken
    {
        private string m_userName = null;
        public  string UserName
        {
            set
            {
                if (!Object.Equals(m_userName,value))
                {
                    Logout();
                    m_userName = value;
                }
            }
            get
            {
                return m_userName;
            }
        }

        private string m_password = null;
        public  string Password
        {
            set
            {
                if (!Object.Equals(m_password, value))
                {
                    Logout();
                    m_password = value;
                }
            }
            get
            {
                return m_password;
            }            
        }

        private string m_school = null;
        public  string School
        {
            set
            {
                if (!Object.Equals(m_school, value))
                {
                    Logout();
                    m_school = value;
                }
            }

            get
            {
                return m_school;
            }
        }

        private Language m_language = Language.English;
        public Language Language
        {
            set
            {
                if(m_language != value)
                {
                    m_language = value;
                    ChangeLanguage();
                }
            }
            get
            {
                return m_language;
            }
        }

        private LogoutType m_logoutType = LogoutType.RequestLogout;
        public LogoutType LogoutType
        {
            set
            {
                m_logoutType = value;
            }
            get
            {
                return m_logoutType;
            }
        }
        
        private CookieContainer m_cookies = null;
        public CookieContainer Cookies
        {
            get
            {
                if (m_cookies == null)
                {
                    Login();
                }
                return m_cookies;
            }
        }

        public LoginToken(String userName, String password, String school)
        {
            this.UserName = userName;
            this.Password = password;
            this.School = school;
        }

        /* sends login request */
        public void Login()
        {
            m_cookies = null;

            LoginPage loginPage = new LoginPage(UserName, Password, School);
            m_cookies = loginPage.Cookies;

            ChangeLanguage();
        }

        /* logs out */
        public void Logout()
        {
            Logout(this.LogoutType);
        }

        /* logs out using one of two available methods */
        public void Logout(LogoutType logoutType)
        {
            if (!IsLoggedIn()) return;

            if (logoutType == LogoutType.RequestLogout)
            {
                LogoutPage logoutPage = new LogoutPage(this);
                logoutPage.Request();
            }

            m_cookies = null;
        }

        public bool IsLoggedIn()
        {
            return m_cookies != null;
        }


        private void ChangeLanguage()
        {
            if (IsLoggedIn())
            {
                LanguagePage langPage = new LanguagePage(Language);
                langPage.Cookies = Cookies;
                langPage.Request();
            }
        }
    }
}
